def main():
    # 定义食物清单（id、名称、类别、单价）
    foods = [
        {"id": 1, "name": "经典汉堡", "category": "主食", "price": 18},
        {"id": 2, "name": "香辣鸡翅", "category": "小吃", "price": 12},
        {"id": 3, "name": "薯条(中)", "category": "小吃", "price": 9},
        {"id": 4, "name": "可乐(中)", "category": "饮品", "price": 6},
        {"id": 5, "name": "牛肉卷", "category": "主食", "price": 22},
        {"id": 6, "name": "冰淇淋", "category": "甜品", "price": 8}
    ]
    
    print("="*30)
    print("欢迎来到美味快餐店！")
    print("="*30)

    # 展示菜单（按类别分组）
    def show_menu():
        print("\n【菜单列表】")
        # 按类别分组展示
        categories = set(food["category"] for food in foods)
        for category in sorted(categories):
            print(f"\n{category}：")
            # 筛选当前类别的食物并按id排序
            category_foods = sorted(
                [f for f in foods if f["category"] == category],
                key=lambda x: x["id"]
            )
            for food in category_foods:
                print(f"  {food['id']}. {food['name']} - {food['price']}元")

    # 选择食物及数量
    def select_foods():
        cart = []  # 购物车：存储选中的食物及数量
        while True:
            try:
                food_id = int(input("\n请输入要购买的食物编号（输入0结束选择）："))
                if food_id == 0:
                    break
                
                # 查找选中的食物
                selected = next((f for f in foods if f["id"] == food_id), None)
                if not selected:
                    print("❌ 无效的食物编号，请重新输入！")
                    continue
                
                # 输入购买数量
                quantity = int(input(f"请输入{selected['name']}的购买数量："))
                if quantity <= 0:
                    print("❌ 数量必须是正整数，请重新输入！")
                    continue
                
                # 添加到购物车
                cart.append({"food": selected, "quantity": quantity})
                print(f"✅ 已添加 {selected['name']} x{quantity} 到购物车")
            
            except ValueError:
                print("❌ 输入错误，请输入数字！")
        
        return cart

    # 计算总价并展示订单
    def show_order(cart):
        if not cart:
            return 0
        
        print("\n" + "="*20)
        print("【您的订单详情】")
        total = 0
        for item in cart:
            food = item["food"]
            qty = item["quantity"]
            subtotal = food["price"] * qty
            total += subtotal
            print(f"{food['name']} x{qty}：{food['price']}元/份，小计：{subtotal}元")
        print("-"*20)
        print(f"订单总价：{total}元")
        print("="*20)
        return total

    # 选择支付方式
    def select_payment(total):
        print("\n【支付方式选择】")
        print("1. 微信支付")
        print("2. 支付宝支付")
        print("3. 现金支付（满50元减5元）")
        
        while True:
            try:
                choice = int(input("请选择支付方式（1-3）："))
                if choice not in [1, 2, 3]:
                    print("❌ 请输入1-3之间的数字！")
                    continue
                
                # 计算最终支付金额
                if choice == 3:
                    if total >= 50:
                        final = total - 5
                        print(f"✅ 现金支付满50减5，优惠后金额：{final}元")
                    else:
                        final = total
                        print(f"✅ 现金支付，金额：{final}元")
                else:
                    final = total
                    method = "微信支付" if choice == 1 else "支付宝支付"
                    print(f"✅ {method}，金额：{final}元")
                
                return final
            
            except ValueError:
                print("❌ 输入错误，请输入数字！")

    # 确认支付
    def confirm_payment(amount):
        while True:
            confirm = input(f"\n是否确认支付 {amount} 元？(y/n)：").lower()
            if confirm == "y":
                print("\n🎉 支付成功！感谢您的购买，祝您用餐愉快！")
                return True
            elif confirm == "n":
                print("\n❌ 订单已取消，欢迎下次光临！")
                return False
            else:
                print("❌ 请输入y（确认）或n（取消）！")

    # 主流程执行
    show_menu()                  # 展示菜单
    cart = select_foods()        # 选择食物
    total = show_order(cart)     # 展示订单
    
    if total == 0:
        print("\n您没有选择任何食物，欢迎下次光临！")
        return
    
    final_amount = select_payment(total)  # 选择支付方式
    confirm_payment(final_amount)         # 确认支付


if __name__ == "__main__":
    main()